package com.payfen.client.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.payfen.client.jsonbean.ResultBean;
import com.payfen.dao.inf.DigitalDataDao;
import com.payfen.ibatis.domain.DigitalData;
import com.payfen.util.FileTool;

@Controller
public class YueyinGetDataInfAction extends Action {
	
	private DigitalDataDao digitalDataDao;

	@RequestMapping(value = "/GetInfoData", method = RequestMethod.GET)
	public void exchangeRateUpdate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String returnMsg = "";
		try {
			String uuid = getRequestParameter(request, "uuid");
			if (uuid != null && uuid.length() > 17) {
				String imsi = uuid.substring(17,uuid.length());
				uuid = uuid.substring(0,17);
				
				Map map = new HashMap();
				map.put("uuid", uuid);
				List digitalList = digitalDataDao.getDigitalDataByUUID(map);
				
				StringBuffer buffer = new StringBuffer();
				if(digitalList != null && digitalList.size() > 0){
					buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
						.append("<payfen>")
							.append("<returncode>").append("0000").append("</returncode>")
							.append("<message></message>")
							.append("<digitallist>");
							
							for(int i = 0 ; i < digitalList.size(); i++){
								DigitalData dd = (DigitalData)digitalList.get(i);
								
								buffer.append("<digital>")
										.append("<title>").append(dd.getTitle()).append("</title>")
										.append("<filelink>").append(dd.getFileLink()).append("</filelink>")
										.append("<filelinktype>").append(dd.getFileLink()).append("</filelinktype>")
									.append("</digital>");
							}
							buffer.append("</digitallist></payfen>");
							returnMsg = buffer.toString();
				}else{
					returnMsg = this.createReturnErrorXml("0001", FileTool.getErrorCodeFromProperties("0001"));
				}
			} else {
				returnMsg = this.createReturnErrorXml("0002", FileTool.getErrorCodeFromProperties("0002"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnMsg = this.createReturnErrorXml("0003", FileTool.getErrorCodeFromProperties("0003"));
		}
		out.write(returnMsg);
		out.flush();
		out.close();
	}

	/**
	 * 生成错误xml
	 * @param code
	 * @param message
	 * @return
	 */
	private String createReturnErrorXml(String code,String message){
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
				.append("<payfen>")
				.append("<returncode>").append(code).append("</returncode>")
				.append("<message>").append(message).append("</message>")
				.append("<digitallist></digitallist>")
			.append("</payfen>");
		
		return buffer.toString();
	}
	
	public void setDigitalDataDao(DigitalDataDao digitalDataDao) {
		this.digitalDataDao = digitalDataDao;
	}
}
